package jrain.fw.http.common.utils;

import java.util.HashMap;

import jrain.fw.core.service.ServiceConst;
import jrain.fw.core.utils.FwUtils;
import jrain.fw.http.common.consts.HttpCfg;
import jrain.utils.collection.RDMap;
import jrain.utils.collection.RMap;
import jrain.utils.lang.LangUtils;
import jrain.utils.lang.StringUtils;

public class HttpCommonUtils {

	public static int getPort() {
		RMap<String, Object> cfg = FwUtils.getCfgService().getCfgMap(ServiceConst.M_HTTP_SERVER, new RDMap<>(new HashMap<>()));
		int port = LangUtils.parseInt(cfg.get(HttpCfg.httpServerPort.getName()));
		// 从配置文件加载，用于支持一个服务器部署不同端口
		if (port == 0) {
			port = LangUtils.parseInt(FwUtils.getFwCfg().get(HttpCfg.httpServerPort.getName()));
		}
		if (port == 0) {
			port = HttpCfg.httpServerPort.getIntValue();
		}
		return port;
	}

	public static String getContext() {
		RMap<String, Object> cfg = FwUtils.getCfgService().getCfgMap(ServiceConst.M_HTTP_SERVER, new RDMap<>(new HashMap<>()));
		String contextPath = StringUtils.trimNull(cfg.get(HttpCfg.httpContextPath.getName()));
		if (contextPath.length() == 0) {
			contextPath = StringUtils.trimNull(FwUtils.getFwCfg().get(HttpCfg.httpContextPath.getName()));
		}
		if (contextPath.length() == 0) {
			contextPath = HttpCfg.httpContextPath.getValue();
		}
		if (!contextPath.startsWith("/")) {
			contextPath = "/" + contextPath;
		}
		if (contextPath.endsWith("/")) {
			contextPath = contextPath.substring(0, contextPath.length() - 1);
		}
		return contextPath;
	}

}
